package org.thesis.activity;


import java.util.ArrayList;

import org.thesis.xuly.XuLyChuoi;

import org.thesis.R;
import android.app.TabActivity;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.util.Log;
import android.widget.TabHost;

public class showMailActivity extends TabActivity
{
	final static String KEY_LIST_USERNAME = "listUsername"; //nhan chuoi string tu listmial gui qua
	final static String KEY_LIST_PASSWORD = "listPassword";
	final static String KEY_USERNAME_LOGIN = "USERNAMELOGIN"; // GUI USERVA PASS CHO BEN MAIL CONTROL LOGIN
	final static String KEY_PASSWORD_LOGIN = "PASSWORDLOGIN";
	ArrayList<String>listUsername = new ArrayList<String>();
	ArrayList<String>listPassword = new ArrayList<String>();
	String username;
	String password;
	XuLyChuoi xulychuoi;
	String tag = "Tag:showMailActivity";
	
	@Override
	protected void onCreate(Bundle savedInstanceState) 
	{
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		
		Bundle bundle = getIntent().getExtras();
		
		if(bundle != null)
		{
			
			listUsername = bundle.getStringArrayList(KEY_LIST_USERNAME);
			listPassword = bundle.getStringArrayList(KEY_LIST_PASSWORD);
		}
		setContentView(R.layout.tabhost);
		Resources res = getResources();
		TabHost tabHost = getTabHost();
		TabHost.TabSpec spec ;
		Intent intent = null;
		String name_tabSpec = null;
		int icon_tabSpec = 0;
		int n_tabSpec = listUsername.size(); //so tabhost se tao dua vao so email luong email login nhan dc
		Log.i("tab h�t", "vao for");
		for(int i = 0 ; i < n_tabSpec; i ++)
		{
			try
			{
				//tao tung tabspec
				username = listUsername.get(i);
				password = listPassword.get(i);
//				Log.v(tag, username + "--" + password);
				//if username la gmail : goi activity GmailControl
				//if username la hotmail: HotmailControl
				//if la yahoo mail goi : yahoomailControl
				// if la mail noi bo : goi noiboControl
				xulychuoi = new XuLyChuoi();
				String loai_email = xulychuoi.kt_loai_email_login(username);
				Log.i("loaiemail ::::", loai_email);
				if(loai_email.equals("gmail.com"))
				{
					// xu ly mail gmail
					Log.i("loaiemail", "tab gmail");
					intent = new Intent(this, org.thesis.activity.getGmailActivity.class);
					name_tabSpec = username;
					icon_tabSpec = R.drawable.gmail_icon;
					
				}else if (loai_email.startsWith("yahoo") || loai_email.equals("yahoo.com") )
				{
					//xu ly mail yahoo
					
					intent = new Intent(this, org.thesis.activity.getYahooActivity.class);
					name_tabSpec = username;
					icon_tabSpec = R.drawable.yahoo_icon;
					Log.i("loaiemail", " tab YAHOO");
				}else if (loai_email.startsWith("hotmail"))
				{
					//xu ly mail hot mail
					Log.i("loaiemail", "tab hot mail");
					intent = new Intent(this, org.thesis.activity.getHotmailActivity.class);
					name_tabSpec = username;
					icon_tabSpec = R.drawable.hotmail_icon;
				}
				else
				{
					//xu ly cac loai mail con lai
					Log.i("loaiemail", "Khac ");
					intent = new Intent(this, org.thesis.activity.getNoiboActivity.class);
					name_tabSpec = username;
					icon_tabSpec = R.drawable.mdaemon;
					
				}
				
				//tab tabhost
				if (name_tabSpec != null && icon_tabSpec!= 0)
				{
					//truyn username va password cho mail tuong ung xhost
					Log.v(tag, "gan tabspec vao tab");
					bundle.putString(KEY_USERNAME_LOGIN, username);
					bundle.putString(KEY_PASSWORD_LOGIN, password);
					intent.putExtras(bundle); // kem vao intent
					spec = tabHost.newTabSpec(name_tabSpec); //ten spec tab
					spec.setIndicator(name_tabSpec, res.getDrawable(icon_tabSpec)); //ten hien thi tren tabhost
					spec.setContent(intent); // gui intent kem username va password
					tabHost.addTab(spec);
					Log.v(tag, "showmailA -> get***mailActivity : " + username );
				}
			}catch (Exception e)
			{
				// TODO: handle exception
				Log.i(tag, "Loi create tablhost "+ e.toString());
			}
		}
	}
}

